package com.tanhua.app.controller;

import com.tanhua.app.service.CommentService;
import com.tanhua.app.service.MovementService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    /**
     * 动态-发布
     */
    @PostMapping
    public ResponseEntity<Object> movements(Movement movement,
                                    MultipartFile[] imageContent){

        // 执行业务成方法
        movementService.saveMovements(movement, imageContent);

        return ResponseEntity.ok(null);
    }

    /**
     * 我的动态
     */
    @GetMapping("/all")
    public ResponseEntity<PageResult<MovementsVo>> userMovement(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                                @RequestParam(required = false, defaultValue = "10") Integer pagesize,
                                                                Long userId) {

        PageResult<MovementsVo> pageResult = movementService.findPageUserId(page, pagesize, userId);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 好友动态
     */
    @GetMapping
    public ResponseEntity<PageResult<MovementsVo>> findFriendMovement(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                                      @RequestParam(required = false, defaultValue = "10") Integer pagesize){

        PageResult<MovementsVo> pageResult = movementService.findFriendMovement(page, pagesize);

        return ResponseEntity.ok(pageResult);
    }


    /**
     * 推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity<PageResult<MovementsVo>> recommend(@RequestParam(defaultValue = "1", required = false) Integer page,
                                                             @RequestParam(defaultValue = "10", required = false) Integer pagesize) {

        PageResult<MovementsVo> pageResult = movementService.findRecommendMovements(page, pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据Id查询动态信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<MovementsVo> findById(@PathVariable("id") String movementId) {

        MovementsVo vo = movementService.findMovementById(movementId);

        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId ) {
        Integer likeCount = commentService.likeComment(movementId);

        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.dislikeComment(movementId);

        return ResponseEntity.ok(likeCount);
    }

    /**
     * 动态喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer loveCount = commentService.loveComment(movementId);

        return ResponseEntity.ok(loveCount);
    }

    /**
     * 取消动态喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer loveCount = commentService.unloveComment(movementId);

        return ResponseEntity.ok(loveCount);
    }


    /**
     * 首页谁看过我
     */
    @GetMapping("/visitors")
    public ResponseEntity findVisitors() {
        List<VisitorsVo> list = movementService.findVisitors();

        return ResponseEntity.ok(list);
    }
}
